package auctionhaus

import com.icegreen.greenmail.util.*

class NotificationIntegrationTests extends GroovyTestCase {
    def mailService
    def greenMail

        void testSendMail() {
            Map mail = [name: "test", description: "test listing", seller: "jeff.lemmerman@gmail.com", winner: "todd.becicka@gmail.com",
                    startingBid: "5.00", winningBidAmount: "7.50", numberOfBids: "2",
                    listingDetail: "http://localhost:8080/AuctionHaus/listing/show/id", subject: "Test"]

            sleep(1000)  //necessary to wait so that greenMail messages from previous test have arrived
            greenMail.deleteAllMessages()     //clear greenMail messages from other tests

            mailService.sendMail {
                to mail.seller, mail.winner
                from 'bids@auctionhaus.com'
                subject mail.subject
                text mail.toString()
            }

            //check sent to winner and seller
            assert (greenMail.getReceivedMessages().length == 2)

            def message = greenMail.getReceivedMessages()[0]

            assertEquals(mail.toString(), GreenMailUtil.getBody(message))
            assertEquals('jeff.lemmerman@gmail.com',GreenMailUtil.getAddressList(message.allRecipients[0]))
            assertEquals('bids@auctionhaus.com', GreenMailUtil.getAddressList(message.from))
            assertEquals('Test', message.subject)
            assert GreenMailUtil.getBody(message).contains("http://localhost:8080/AuctionHaus/listing/show/id")
            assert GreenMailUtil.getBody(message).contains("winningBidAmount")
        }

        void tearDown() {
            greenMail.deleteAllMessages()
        }
}
